from django.http import HttpResponse
import json
from helpers.ge.long_link import long_link,func_list
from models import WashSessionModel,ZKWashLog,CarWashLocalModel,UserCarModel
import meals
from django.views.decorators.csrf import csrf_exempt
from ex import hook_all,redis_conn

def list_func(request):
    return HttpResponse(unicode(func_list))
           
def session_check(request):
    session_id= request.GET.get('session')
    code=request.GET.get('code')
    try:
        session=WashSessionModel.objects.get(pk=session_id)
        new_code=check_state(session, code)
        #session=WashSessionModel.objects.get(pk=session_id)
        #log = ZKWashLog.objects.filter(session=session).order_by('-update_at').first()
        if new_code:
            dc={
                'status':'success',
                'code':new_code,
            }            
        else:
            log = ZKWashLog.objects.filter(session=session).order_by('-update_at').first()
            if log:
                dc={
                    'status':'success',
                    'code':log.code
                }
            else:
                dc={
                    'status':'success',
                    'msg':"can't find washstate record with this session",
                }  
            

      
    except WashSessionModel.DoesNotExist:
        dc={
            'status':'error',
            'msg':'session id not exist'
        }
    return HttpResponse(json.dumps(dc),content_type="application/json")

@long_link(span=30)
def check_state(session,org_code):
  
    log = ZKWashLog.objects.filter(session=session).order_by('-update_at').first()
    if log:
        if not org_code:
            return log.code
        if log.code !=org_code:
            return log.code
        
@csrf_exempt
@hook_all
def new_session(request):
    if request.method=='GET':
        chepai=request.GET.get('chepai')
        order_key = request.GET.get('order_key')
        zk_no=request.GET.get('zk')
    else:
        dc=json.loads(request.body)
        chepai=dc.get('chepai')
        order_key = dc.get('order_key')
        zk_no=dc.get('zk')        
    try:
        order=None
        zk = CarWashLocalModel.objects.get(no=zk_no)
        if chepai:
            redis_conn.set('__last_car_no_'+zk_no,chepai,ex=60*5)
            car = UserCarModel.objects.get(car_no=chepai)
            order =order_with_chepai(chepai)
            
        elif order_key:
            order = order_with_key(order_key)
            car = order.car
        if not order:
            raise UserWarning,'can not find active order'
        session=WashSessionModel.objects.create(order=order,zk=zk,car=car)
        dc={
            'status':'success',
            'session':session.pk
        }
    except UserWarning as e:
        dc={
            'status':'fail',
            'msg':unicode(e)
        }
    except CarWashLocalModel.DoesNotExist:
        dc={
            'status':'fail',
            'msg':'zk number not registed'
        }
    except UserCarModel.DoesNotExist:
        dc={
            'status':'fail',
            'msg':'can not find match car'
        }
    return HttpResponse(json.dumps(dc),content_type="application/json")

@long_link(span=30)
def order_with_chepai(chepai):
    try:
        order = meals.consume_chepai(chepai)
        return order
    except UserWarning as e:
        return None

@long_link(span=30)
def order_with_key(order_key):
    try:
        order = meals.consume_order_key(order_key)
        return order
    except UserWarning as e:
        return None



def active_session(request):
    chepai=request.GET.get('chepai')
    if not chepai:
        raise UserWarning,'argument need chepai'
    try:
        # car = UserCarModel.objects.get(car_no=chepai)
        session=check_active_session(car_no=chepai)
        if session:
            dc={
                'status':'success',
                'session':session.pk,
            }
        else:
            dc={
                'status':'success',
                'session':None
            }
    except UserCarModel.DoesNotExist:
        dc={
            'status':'fail',
            'msg':'please input registed chepai'
        }
    return HttpResponse(json.dumps(dc),content_type="application/json")

@long_link(span=30)
def check_active_session(car_no):
    session=WashSessionModel.objects.filter(car__car_no=car_no).exclude(zkwashlog__code=17002).first()
    return session